package com.emm.yixun.website.controller.marketing;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.emm.yixun.common.excel.FastExcel;
import com.emm.yixun.common.exception.BusinessException;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.MarketingModel;
import com.emm.yixun.common.model.dto.MarketingModel_CW;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
import com.emm.yixun.website.service.marketing.MarketingImpService;
import com.emm.yixun.website.vo.CustomerImportVo;

/**
 * 交易数据导入
 * @author junze.liu
 *
 */
@RequestMapping(value = "/marketing/import/")
@Controller
public class MarketingImpController extends BaseController<Object>{
	
	protected Logger logger = LoggerFactory.getLogger(MarketingImpController.class);
	
	@Autowired
	private MarketingImpService marketingImpService;
	
	/**
	 * 导入认购数据
	 * @param request
	 * @param multipartFile
	 * @param response
	 * @param projectId
	 * @return
	 */
	@RequestMapping(value = "subscribeImport")
	@ResponseBody
	public AjaxRes subscribeImport(HttpServletRequest request,
			@RequestParam("multipartFile")MultipartFile multipartFile,HttpServletResponse response,Long projectId, int type) {
		AjaxRes ar = getAjaxRes();
		response.setContentType("text/html; charset=utf-8");
		
		FastExcel fastExcel = null;
		try {
			User user = ShiroUtil.getCurrentUser();
			//解析Excel数据并转为java实体bean
			fastExcel = new FastExcel(multipartFile.getInputStream());
			//获取认购标签的数据
			fastExcel.setSheetName("认购");
			//从第1行获取（标题为第0行）
			fastExcel.setStartRow(1);
			List<MarketingModel> list = new ArrayList<MarketingModel>();
			if(type == 2) {
				//当模板为车位时，将车位模板转为通用模板
				List<MarketingModel_CW> list_cw = fastExcel.parse(MarketingModel_CW.class);
				for(MarketingModel_CW cw : list_cw) {
					MarketingModel mm = new MarketingModel();
					BeanUtils.copyProperties(mm, cw);
					list.add(mm);
				}
			} else {
				list = fastExcel.parse(MarketingModel.class);
			}
			if(list == null || list.size() == 0) {
				ar.setFailMsg("未获取到需要导入的认购数据");
				return ar;
			}
			//新增认购
			Response<CustomerImportVo> res = marketingImpService.addSubscribe(list, projectId, user, type);
			if(res.isSuccess()) {
				ar.setSucceed(res.getEntity());
			}
		} catch (BusinessException e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(e.getErrorMsg());
		} catch (Exception e) {
			logger.error("认购数据导入失败", e);
			ar.setFailMsg(e.getMessage());
		} finally {
			try {
				fastExcel.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ar;
	}
	
	/**
	 * 导入签约数据
	 * @param request
	 * @param multipartFile
	 * @param response
	 * @param projectId
	 * @return
	 */
	@RequestMapping(value = "signedImport")
	@ResponseBody
	public AjaxRes signedImport(HttpServletRequest request,
			@RequestParam("multipartFile")MultipartFile multipartFile,HttpServletResponse response,Long projectId, int type) {
		AjaxRes ar = getAjaxRes();
		response.setContentType("text/html; charset=utf-8");
		
		FastExcel fastExcel = null;
		try {
			User user = ShiroUtil.getCurrentUser();
			//解析Excel数据并转为java实体bean
			fastExcel = new FastExcel(multipartFile.getInputStream());
			//获取认购标签的数据
			fastExcel.setSheetName("签约");
			//从第1行获取（标题为第0行）
			fastExcel.setStartRow(1);
			List<MarketingModel> list = new ArrayList<MarketingModel>();
			if(type == 2) {
				//当模板为车位时，将车位模板转为通用模板
				List<MarketingModel_CW> list_cw = fastExcel.parse(MarketingModel_CW.class);
				for(MarketingModel_CW cw : list_cw) {
					MarketingModel mm = new MarketingModel();
					BeanUtils.copyProperties(mm, cw);
					list.add(mm);
				}
			} else {
				list = fastExcel.parse(MarketingModel.class);
			}
			if(list.size() == 0) {
				ar.setFailMsg("未获取到需要导入的签约数据");
				return ar;
			}
			//新增签约
			Response<CustomerImportVo> res = marketingImpService.addSigned(list, projectId, user, type);
			if(res.isSuccess()) {
				ar.setSucceed(res.getEntity());
			}
		} catch (BusinessException e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(e.getErrorMsg());
		} catch (Exception e) {
			logger.error("签约数据导入失败", e);
			ar.setFailMsg(e.getMessage());
		} finally {
			try {
				fastExcel.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ar;
	}
}
